package com.ims.controller.system.grade;

import com.ims.controller.base.BaseController;
import com.ims.entity.system.Grade;
import com.ims.service.GradeService;
import com.ims.shiro.ShiroSessionService;
import com.ims.util.PageData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/grade")
public class GradeController extends BaseController{
    @Resource
    private GradeService gradeService;
    @Resource
    ShiroSessionService session;

    @RequestMapping("/gradetable")
    public ModelAndView gradetable() throws Exception{
        ModelAndView mv=this.getModelAndView();
        String uname=session.getSession().getAttribute("USERNAME").toString();

        if(uname.equals("admin")||uname.equals("scu")){
            List<Grade> gradeList=gradeService.gradelist();
            mv.addObject("gradeList",gradeList);
            mv.setViewName("system/grade/checkgrade");
        }else{
            List<Grade> gradeList=gradeService.listGradeByUname(uname);
            mv.addObject("gradeList",gradeList);
            mv.setViewName("system/grade/checkgrade");
        }

        return mv;
    }


    @RequestMapping("/modifygradetable")
    public ModelAndView modifygradetable() throws Exception{
        ModelAndView mv=this.getModelAndView();
        String uname=session.getSession().getAttribute("USERNAME").toString();

        if(uname.equals("admin")||uname.equals("scu")){
            List<Grade> gradeList1=gradeService.gradelist();
            mv.addObject("gradeList1",gradeList1);
            mv.setViewName("system/grade/modifygrade");
        }else{
            List<Grade> gradeList1=gradeService.listGradeByUname(uname);
            mv.addObject("gradeList1",gradeList1);
            mv.setViewName("system/grade/modifygrade");
        }

        return mv;
    }

    @RequestMapping(value = "/updateGrade")
    public ModelAndView updateGrade()throws Exception{
        ModelAndView mv=this.getModelAndView();
        PageData pd=new PageData();
        pd = this.getPageData();

        Grade grade=new Grade();
        grade.setUname(pd.getString("uname"));
        grade.setCno(Integer.parseInt(pd.getString("cno")));
        grade.setGrade(Integer.parseInt(pd.getString("grade")));

        gradeService.updateGrade(grade);
        mv.addObject("msg","success");
        mv.setViewName("system/grade/modifygrade");
        return mv;

    }
}
